<template>
  <div class="main-wrap">
    <div class="operation-wrap">
      <el-button type="primary" size="mini" @click="openDia()">新增</el-button>
    </div>
    <el-table border :data="listData" :row-style="{ height: '40px' }" style="width: 100%">
      <el-table-column label="图片" width="180">
        <template slot-scope="scope">
          <div>
            <img
              style="width: 100%; height: 100px%"
              v-if="scope.row.image"
              :src="'//127.0.0.1:9003/' + scope.row.image"
            />
          </div>
        </template>
      </el-table-column>
      <el-table-column label="图书馆" prop="libraryName"></el-table-column>
      <el-table-column label="楼层名称" prop="name"></el-table-column>
      <el-table-column label="容纳人数" prop="capacity"></el-table-column>
      <el-table-column label="楼层级别" prop="sort"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="openDia(scope.row)"> 编辑 </el-button>
          <el-button type="text" size="mini" @click="delFloor(scope.row)"> 删除 </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="fromData.id ? '修改' : '新增'" :visible.sync="dialogVisible" width="30%">
      <el-form
        :model="fromData"
        status-icon
        ref="dataForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="图书馆" prop="libId">
          <el-select v-model="fromData.libId">
            <el-option
              v-for="item in libraryList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="楼层名称" prop="name">
          <el-input v-model="fromData.name"></el-input>
        </el-form-item>
        <el-form-item label="楼层级别" prop="sort">
          <el-input v-model.number="fromData.sort"></el-input>
        </el-form-item>
        <el-form-item label="容纳人数" prop="capacity">
          <el-input v-model.number="fromData.capacity"></el-input>
        </el-form-item>
        <el-form-item label="图片" prop="image">
          <el-upload
            class="avatar-uploader"
            action="//127.0.0.1:9003/file/uploadimg"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="fromData.image" :src="'//127.0.0.1:9003/' + fromData.image" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import request from '@/req'
export default {
  data() {
    return {
      listData: [],
      fileList: [],
      fromData: {
        id: '',
        name: '',
        image: '',
        sort: '',
        capacity: '',
        libId: '',
      },
      dialogVisible: false,
      libraryList: [],
    }
  },
  created() {
    this.getListData()
  },
  methods: {
    async getListData() {
      const res = await request.get('/floor/getFloorList')
      this.listData = res
    },
    openDia(data) {
      this.getLibrary()
      this.dialogVisible = true
      this.fromData.id = ''
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (data) {
          Object.assign(this.fromData, data)
        }
      })
    },
    async getLibrary() {
      const res = await request.get('/library/getLibraryList')
      this.libraryList = res
    },
    async submitForm() {
      await request.post('/floor/submit', this.fromData)
      this.dialogVisible = false
      this.getListData()
    },
    delFloor(data) {
      request.post('/floor/delFloor', { id: data.id }).then((res) => {
        if (res.code == 200) {
          this.$message({
            type: 'success',
            message: '删除成功',
          })
          this.getListData()
        } else {
          this.$message({
            type: 'warning',
            message: res.msg,
          })
        }
      })
    },
    handleAvatarSuccess(res, file) {
      this.fromData.image = res
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
  },
}
</script>

<style scoped>
:deep(.avatar-uploader .el-upload) {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
:deep(.avatar-uploader .el-upload:hover) {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
